// pages/myapiary/details/details.js
import http from '../../../http/http.js'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    fc: true,
    fx: false,
    yj: false,
    showFc: true,
    screenHeight: getApp().globalData.screenHeight - 260,
    markers: [],
    boobox:{},
    id: '',
    ids:'',
    info: '',
    mapshow:false,
    weather:{},
    date:'',
    time:'',
    warmlist:{},
    alertpop:false,
    popmes:'',
    myapiary:'',
    isFload: true
  },

  /**
   * 生命周期函数--监听页面加载
   */
  // 下拉刷新
  onPullDownRefresh: function () {
    http.getdata('/api/beehive/list?apiaryId=' + this.data.id).then((res) => {
      this.setData({
        boobox: res.data.result
      })
      wx.stopPullDownRefresh()
    })
    
  },
  onLoad: function(options) {
    console.log(options)
    this.data.id = options.id
    this.setData({
      myapiary: options.myapiary,
      id: options.id
    })
    if (options.type) {
      this.setData({
        fc: false,
        fx: true,
        yj: false,
      })
    }
    if (options.myapiary) {
      http.getdata('/api/beehive/list?apiaryId=' + this.data.id).then((res) => {
        this.setData({
          boobox: res.data.result
        })
      })
    } else {
      this.beebox()
    }
    this.getapiinfo()
    this.warmlist()
  },
  fload() {
    //改变isFload的状态
    this.setData({
      isFload: !this.data.isFload
    })
  },
  onShow:function(){
    this.getapiinfo()
  },
  // 获取天气
  getweather:function(lat,lng){
    http.getdata('/api/weather/detail?lat='+lat+'&lng='+lng).then((res)=>{
      this.setData({
        weather: res.data.result
      })
      let re = res.data.result.reporttime
      this.setData({
        date: re.split(' ')[0],
        time: re.split(' ')[1]
      })
    })
  },
  // 蜂箱列表
  beebox:function(){
    http.getdata('/api/beehive/list').then((res) => {
      this.setData({
        boobox: res.data.result
      })
      wx.stopPullDownRefresh();
    })
  },
  // 添加
  toaddbeebox:function(){
    wx.navigateTo({
      url: '../../beebox/add/addbeebox?showtoast=' + true
    })
  },
  // 预警列表
  warmlist:function(){
    http.getdata('/api/monitor/list?status=1&apiaryId='+this.data.id).then((res) => {
      this.setData({
        warmlist:res.data.result
      })
    })
  },
  // 蜂场信息
  getapiinfo: function() {
    let _this=this
    let maker = []
    http.getdata('/api/apiary/get?id=' + this.data.id).then((res) => {
      this.setData({
        info: res.data.result,
        lat: res.data.result.lat,
        lng: res.data.result.lng,
      })
      maker.push({
        iconPath: "../../../images/map_icon.png",
        id: 0,
        latitude: res.data.result.lat,
        longitude: res.data.result.lng,
        width: 30,
        height: 30
      })
      this.setData({
        markers: maker
      })
      this.getweather(res.data.result.lat, res.data.result.lng)
      setTimeout(function(){
        _this.setData({
          mapshow:true
        })
      },2000)
    })
  },
  fc_tab: function() {
    this.setData({
      fc: true,
      fx: false,
      yj: false,
    })
  },
  fx_tab: function() {
    this.setData({
      fc: false,
      fx: true,
      yj: false,
    })
    
  },
  yj_tab: function() {
    this.setData({
      fc: false,
      fx: false,
      yj: true,
    })
  },
  bee_box_details: function(e) {
    let val = e.currentTarget.dataset.devicecode
    wx.navigateTo({
      url: '../../beebox/detail/index?deviceCode='+val,
    })
  },
  viewbee: function (e) {
    let idd = e.currentTarget.dataset.id
    let mesage = e.currentTarget.dataset.message
    this.setData({
      alertpop: true,
      popmes: mesage,
      ids: idd
    })
  },
  // 关闭查看
  closepop: function () {
    let pars = {
      id: this.data.ids
    }
    http.putdata('/api/monitor/process', pars).then((res) => {
      this.setData({
        alertpop: false
      })
      this.warmlist()
    })
  },
  // 编辑蜂场
  to_edit:function(){
    wx.navigateTo({
      url: '../edit/edit?id='+this.data.id,
    })
  }
})